﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ly.Web.QianTai
{
    public partial class discussWork : System.Web.UI.Page
    {
        ly.Model.Comment comModel = new Model.Comment();
        ly.BLL.WorkInfo workBll = new BLL.WorkInfo();
        ly.BLL.Comment comBll = new BLL.Comment();
        public string workintroduction = "";
        public string workname = "";
        public string username = "";
        public string WorkPicture = ""; 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                initLogin();
                BindData();
            }
        }
        public void initLogin()//初始化用户登录
        {
            if (Session["username"] != null)//判断用户是否登录
            {
                lblUserName.Text = Session["username"].ToString();
                pnlLogin.Visible = false;
                pnlLoginOut.Visible = true;
            }
            else
            {
                pnlLogin.Visible = true;
                pnlLoginOut.Visible = false;
            }
        }

        private void BindData()
        {
            string workid = Request["workid"];
            int id = Convert.ToInt32(workid);
            ly.Model.WorkInfo model = workBll.GetModel(id); //根据作品编号获取作品实体
            workintroduction = "<p>" + model.WorkIntroduction + "</p>";
            workname = model.WorkName;
            username = model.UserName;
            WorkPicture = model.WorkPicture;

            string stwhere = "";
            //string strwhere = "where Type in( ) username like '%" + username + "%'";
            DataTable dt = comBll.GetListByid(id).Tables[0];
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dt.DefaultView;
            ps.AllowPaging = true;
            rptDiss.DataSource = ps;
            rptDiss.DataBind();
        }

        protected void btnaddDis_Click(object sender, EventArgs e)
        {
            //string username = Convert.ToString(Session["username"]); //???????parse????
            //string content = txtconnect.Text.Trim();
            //string workid = Request["workid"];
            //int id = Convert.ToInt32(workid);

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script>alert('不登录无法完成评论，请您点击右上角登陆后重新评论');</script>");
                //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('不登录无法完成评论，请您注册账号后重新评论');</script>");
            }
            else 
            {
                string workid = Request["workid"];
                int id = Convert.ToInt32(workid);
                comModel.WorkID = id;
                comModel.CommentContent = txt1.Text.Trim();
                string a = Convert.ToString(Session["username"]);
                comModel.UserName = a;
                comModel.CommentTime = DateTime.Now;//获取当前时间
                int flag = comBll.Add(comModel);
                if (flag > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('评论成功');</script>");
                    BindData();
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('评论失败，请重新评论');</script>");
                }

            }


        }





    }
}